﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerHopDongKhoan: IHopDongKhoan
    {
        public HopDongKhoan GetHopDongKhoan(int oid)
        {
            string sql = @" Select  a.SoHopDong, a.NgayKy, a.ChucVuNguoiKy, a.NguoiKy, a.NguoiLaoDong, a.DiaDiemLamViec, a.ChucDanhChuyenMon, a.LoaiHopDong,
                                    b.QuanLyHopDong, b.HinhThucHopDong, b.TuNgay, b.DenNgay,
                                    c.SoTien, c.HinhThucThanhToan
                            From HopDong a inner join HopDongNhanVien b on a.Oid = b.Oid
                                            inner join HopDongKhoan c on b.Oid = c.Oid
                            Where c.Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            HopDongKhoan hopDongKhoan = new HopDongKhoan();
            
            hopDongKhoan.Oid = oid;
            hopDongKhoan.SoHopDong = row["SoHopDong"].ToString();
            hopDongKhoan.NgayKy = DateTime.Parse(row["NgayKy"].ToString());
            int id;
            if (int.TryParse(row["ChucVuNguoiKy"].ToString(), out id))
            {
                SqlServerChucVu sqlServerChucVu = new SqlServerChucVu();
                hopDongKhoan.ChucVuNguoiKy = sqlServerChucVu.GetChucVu(id);
            }
            if (int.TryParse(row["NguoiKy"].ToString(), out id))
            {
                SqlServerNhanVien sqlServerNhanVien = new SqlServerNhanVien();
                hopDongKhoan.NguoiKy = sqlServerNhanVien.GetNhanVien(id);
            }
            if (int.TryParse(row["NguoiLaoDong"].ToString(), out id))
            {
                //SqlServerHoSo sqlServerHoSo = new SqlServerHoSo();
                hopDongKhoan.NguoiLaoDong = HoSoFactory.GetHoSo(id);
            }
            if (int.TryParse(row["DiaDiemLamViec"].ToString(), out id))
            {
                SqlServerBoPhan sqlServerBoPhan = new SqlServerBoPhan();
                hopDongKhoan.DiaDiemLamViec = sqlServerBoPhan.GetBoPhan(id);
            }
            hopDongKhoan.ChucDanhChuyenMon = row["ChucDanhChuyenMon"].ToString();

            //Hợp đồng khoán
            hopDongKhoan.SoTien = decimal.Parse(row["SoTien"].ToString());
            byte hinhThucThanhToan;
            if (byte.TryParse(row["HinhThucThanhToan"].ToString(), out hinhThucThanhToan))
            {
                hopDongKhoan.HinhThucThanhToan = (EHinhThucThanhToan)hinhThucThanhToan;
            }
            return hopDongKhoan;
        }

        public int Insert(HopDongKhoan hopDongKhoan)
        {
            SqlServerHopDongNhanVien sqlServerHopDongNhanVien = new SqlServerHopDongNhanVien();
            int oid = sqlServerHopDongNhanVien.Insert((HopDongNhanVien)hopDongKhoan);

            if (oid > 0)
            {
                string sql = @" Insert into HopDongKhoan(Oid, SoTien, HinhThucThanhToan)
                            Values(@Oid, @SoTien, @HinhThucThanhToan)";

                SqlParameter[] param = {
                                       new SqlParameter("@Oid", oid),
                                       new SqlParameter("@SoTien", hopDongKhoan.SoTien),
                                       new SqlParameter("@HinhThucThanhToan", hopDongKhoan.HinhThucThanhToan)
                                   };
                int temp = DataProvider.Insert(sql,false, param);
                if (oid > 0)
                {
                    ThamChieuDuLieu thamChieuDuLieu = new ThamChieuDuLieu();
                    thamChieuDuLieu.Cha = new SqlServerKieuDuLieu().GetKieuDuLieu(typeof(HopDong).ToString());
                    thamChieuDuLieu.Con = new SqlServerKieuDuLieu().GetKieuDuLieu(typeof(HopDongKhoan).ToString());
                    thamChieuDuLieu.GiaTri = oid;

                    SqlServerThamChieuDuLieu sqlServerThamChieuDuLieu = new SqlServerThamChieuDuLieu();
                    temp = sqlServerThamChieuDuLieu.Insert(thamChieuDuLieu);

                }
            }
            return 0;
        }

        public int Update(HopDongKhoan hopDongKhoan)
        {
            SqlServerHopDongNhanVien sqlServerHopDongNhanVien = new SqlServerHopDongNhanVien();
            int temp = sqlServerHopDongNhanVien.Update((HopDongNhanVien)hopDongKhoan);

            string sql = @" Update HopDongKhoan
                            Set SoTien = @SoTien,
                                HinhThucThanhToan = @HinhThucThanhToan
                            Where Oid = @Oid";
            SqlParameter[] param = {
                                       new SqlParameter("@Oid", hopDongKhoan.Oid),
                                       new SqlParameter("@SoTien", hopDongKhoan.SoTien),
                                       new SqlParameter("@HinhThucThanhToan", hopDongKhoan.HinhThucThanhToan)
                                   };
            return DataProvider.Update(sql, param);
        }

        public int Delete(HopDongKhoan hopDongKhoan)
        {
            string sql = @" Delete From HopDongKhoan Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", hopDongKhoan.Oid);
            int temp = DataProvider.Update(sql, param);

            if (temp > 0)
            {

                SqlServerHopDongNhanVien sqlServerHopDongNhanVien = new SqlServerHopDongNhanVien();
                temp = sqlServerHopDongNhanVien.Delete((HopDongNhanVien)hopDongKhoan);

                if (temp > 0)
                {
                    ThamChieuDuLieu thamChieuDuLieu = new ThamChieuDuLieu();
                    thamChieuDuLieu.GiaTri = hopDongKhoan.Oid;

                    SqlServerThamChieuDuLieu sqlServerThamChieuDuLieu = new SqlServerThamChieuDuLieu();
                    temp = sqlServerThamChieuDuLieu.Delete(thamChieuDuLieu);

                }
            }
            return temp;
        }


        public int Save(HopDongKhoan obj)
        {
            throw new NotImplementedException();
        }
    }
}
